<?php

namespace App\JsonRpc\ServiceProvider;

use Hyperf\RpcServer\Annotation\RpcService;
use App\Utils\ApiUtils;
use App\Utils\ServerUtil;
use Hyperf\Utils\ApplicationContext;
use App\Constants\ErrorCode;

/**
 * 注意，如希望通过服务中心来管理服务
 * @RpcService(name="UserAmountService", protocol="jsonrpc", server="jsonrpc")
 */
class UserAmountService implements UserAmountServiceInterface
{
    // 统计用户充值金额
    public function useRecharge(string $data ): string
    {
        $data = json_decode($data,true);
        ServerUtil::info('个人充值金额统计',['path'=>'/api/user','parameter'=>$data,'result'=>[]]);
        $type = $data['type'] ?? '';
        $openid = $data['openid'] ?? '';
        if(empty($type) || empty($openid)){   
            return json_encode(ApiUtils::send(100,'请求参数不对'));
        }
        $container = ApplicationContext::getContainer();
        $obj = $container->get(Recharge::class);

        switch ($type)
        {
            case 1: 
                $res = $obj->statistics($openid);
                ServerUtil::info('个人充值金额统计',['path'=>'/api/user','parameter'=>$openid,'result'=>$res]);
                return json_encode(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$res));
                break;
            default:
                //$payload->reply(json_encode(['error'=>0,'msg'=>'sucess','mobile'=>$data['mobile']??''])); 

        }
       
        return '';
    }
}